#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

namespace my_log
{
    namespace Util
    {
        struct Data
        {
        
            static std::string GetData()
            {
                struct tm t;
                time_t _time = time(nullptr);
                localtime_r(&_time, &t);
                std::string ret;
                ret.append(std::to_string(t.tm_year + 1900));
                ret.append("-");
                ret.append(std::to_string(t.tm_mon + 1));
                ret.append("-");
                ret.append(std::to_string(t.tm_mday));
                ret.append(" ");
                ret.append(std::to_string(t.tm_hour));
                ret.append(":");
                ret.append(std::to_string(t.tm_min));
                ret.append(":");
                ret.append(std::to_string(t.tm_sec));
                return ret;
            }

            static void Test()
            {
                std::cout << GetData() << std::endl;
            }

        };    
        
        
        class File
        {
        public:
            static bool FileIsExist(std::string filename)
            {
                struct stat st;
                if (stat(filename.c_str(), &st) < 0)
                {
                    return false;
                }
                return true;
            }

            static std::string GetPath(std::string path)
            {
                int pos = path.find_last_of("/\\");
                if (pos == std::string::npos)
                {
                    return ".";
                }
                return path.substr(0, pos + 1);
            }

            static void CreateDirectory(std::string path)
            {
                //注意：mkdir创建目录必须是一层一层的创建， 如果父目录不存在， 那么无法创建成功
                int index = 0;
                while (index < path.size())
                {
                    int pos = path.find_first_of("/\\", index); //从index位置开始找，index就是目前能确定的创建目录会成功的位置
                    if (pos == std::string::npos) 
                    {
                        mkdir(path.c_str(), 0777);   //找到末尾了， 直接创建就行， 因为index之前的目录已经存在了
                        break;
                    }
                    std::string spath = path.substr(0, pos + 1);
                    if (FileIsExist(spath.c_str()))
                    {
                        index = pos + 1;
                    }
                    else
                    {
                        mkdir(spath.c_str(), 0777);
                        index = pos + 1;
                    }
                }
            }


        public:
            static void Test()
            {
                std::cout << my_log::Util::File::FileIsExist("logfile.txt") << std::endl;
                if (!FileIsExist("logfile.txt"))
                {
                    my_log::Util::File::CreateDirectory("Log/logfile.txt");
                }

                std::cout << my_log::Util::File::GetPath("Log/logfile.txt") << std::endl;
            }
        };

    }
}